package com.ruoyi.amuse.members.service.impl;


import com.ruoyi.amuse.members.domain.ClubMembers;
import com.ruoyi.amuse.members.mapper.ClubMembersMapper;
import com.ruoyi.amuse.members.service.IClubMembersService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 社团成员Service业务层处理
 * 
 * @author juwujie
 * @date 2024-07-19
 */
@Service
public class    ClubMembersServiceImpl implements IClubMembersService
{
    @Autowired
    private ClubMembersMapper clubMembersMapper;



    /**
     * 查询社团成员
     * 
     * @param id 社团成员主键
     * @return 社团成员
     */
    @Override
    public ClubMembers selectClubMembersById(Long id)
    {
        return clubMembersMapper.selectClubMembersById(id);
    }

    /**
     * 查询社团成员列表
     * 
     * @param clubMembers 社团成员
     * @return 社团成员
     */
    @Override
    public List<ClubMembers> selectClubMembersList(ClubMembers clubMembers)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userAccount = loginUser.getSysUser().getUserAccount();
        List<ClubMembers> clubMembers1 = clubMembersMapper.selectClubMembersList(new ClubMembers(null, null, userAccount, null, null));
        for (ClubMembers clubMembers2 : clubMembers1){
            if (clubMembers2.getRoleId() == null){
                continue;
            }
            if (clubMembers2.getRoleId() == 1){
                clubMembers.setClubId(clubMembers2.getClubId());
                //        将currentMember字段更新为clubMembers中当前成员人


                return clubMembersMapper.selectClubMembersList(clubMembers);
            }else {
                continue;
            }
        }

        return clubMembersMapper.selectClubMembersList(clubMembers);
    }

    /**
     * 新增社团成员
     * 
     * @param clubMembers 社团成员
     * @return 结果
     */
    @Override
    public int insertClubMembers(ClubMembers clubMembers)
    {
//        社团成员不能加入同一个社团
        List<ClubMembers> clubMembers1 = selectClubMembersList(clubMembers);
        for (ClubMembers clubMembers2 : clubMembers1){
            if (clubMembers2.getClubId().equals(clubMembers.getClubId()) && clubMembers2.getUserAccount().equals(clubMembers.getUserAccount())){
                System.out.println("该成员已加入该社团");
                return 0;
            }
        }
        return clubMembersMapper.insertClubMembers(clubMembers);
    }

    /**
     * 修改社团成员
     * 
     * @param clubMembers 社团成员
     * @return 结果
     */
    @Override
    public int updateClubMembers(ClubMembers clubMembers)
    {
        return clubMembersMapper.updateClubMembers(clubMembers);
    }

    /**
     * 批量删除社团成员
     * 
     * @param ids 需要删除的社团成员主键
     * @return 结果
     */
    @Override
    public int deleteClubMembersByIds(Long[] ids)
    {
        return clubMembersMapper.deleteClubMembersByIds(ids);
    }

    /**
     * 删除社团成员信息
     * 
     * @param id 社团成员主键
     * @return 结果
     */
    @Override
    public int deleteClubMembersById(Long id)
    {
        return clubMembersMapper.deleteClubMembersById(id);
    }


}
